<template>
  <PageWrapper dense contentFullHeight class="flex">
    <BasicModelTable>
      <template #headBtn="{ tableItems, tableFn }">
        <ButtonActions @custom="handleCreate" :tableFn="tableFn" :data="tableItems[0]" />
      </template>

      <template #actions="{ actions, comParams, record, reload, setModalPropsFn, openModalFn }">
        <TableAction
          :actions="actionsFn(actions, { comParams, record, reload, setModalPropsFn, openModalFn })"
        />
      </template>
    </BasicModelTable>
    <FlowChatReview @register="registerFlowChatReview" />
    <FormTempModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>

<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { PageWrapper } from '/@/components/Page';

  import { TableAction, ActionItem } from '/@/components/Table';
  import { BasicModelTable } from '/@/components/ModelTable';
  import ButtonActions from '/@/components/ModelTable/src/components/ButtonActions.vue';

  import FlowChatReview from './components/FlowChatReview.vue';
  import FormTempModal from './components/FormTempModal.vue';

  import { useModal } from '/@/components/Modal';

  import {
    customRowFnForApi,
    getRowKeys,
    commonDel,
  } from '/@/components/ModelTable/src/hooks/functionSet';
  import { BTN_EVENT_TYPE } from '/@/views/config-center/model-design/model';

  export default defineComponent({
    name: 'FlowTemp',
    components: {
      PageWrapper,
      BasicModelTable,
      TableAction,
      FlowChatReview,
      FormTempModal,
      ButtonActions,
    },
    props: [],
    setup() {
      const data = ref({});
      const [registerFlowChatReview, { openModal: openFlowChatReview }] = useModal();
      const [registerModal, { openModal }] = useModal();
      // { openModal, setModalProps }
      let tabFn: any = {};
      function actionsFn(
        btns: Indexable[],
        { comParams, record, setModalPropsFn, openModalFn, reload },
      ): ActionItem[] {
        const { primaryKey = '', moduleId, modelId } = comParams;
        const params = getRowKeys(primaryKey, record);
        const ids = { moduleId, modelId };
        const ret = btns.map((e) => {
          switch (e.eventType) {
            case BTN_EVENT_TYPE.EDIT:
              e.onClick = async function () {
                const item = {
                  ...e,
                  isUpdate: true,
                  params,
                  record,
                };
                setModalPropsFn && setModalPropsFn(item);
                openModalFn && openModalFn(item);
              };
              return e;
            case BTN_EVENT_TYPE.DELETE:
              return commonDel({ source: e, ids, params }, reload);

            case BTN_EVENT_TYPE.DETAIL:
              e.onClick = async function () {
                const item = {
                  ...e,
                  isUpdate: true,
                  params,
                  record,
                };
                openFlowChatReview(true, item);
              };
              return e;
            case BTN_EVENT_TYPE.CUSTOM:
              e.onClick = customRowFnForApi.bind(null, e, {
                record,
                primaryKeys: params,
              });
              return e;
            default:
              break;
          }

          return e;
        });
        return ret;
      }
      const handleCreate = (_, tabFns) => {
        openModal();
        tabFn = tabFns;
      };
      function handleSuccess() {
        tabFn!.reload();
      }
      return {
        data,
        actionsFn,
        registerFlowChatReview,
        registerModal,
        handleSuccess,
        handleCreate,
      };
    },
  });
</script>
<style lang="less" scoped></style>
